/* concealed carry data cleaning .do file
files used: gunpolicy.dta
files created: cc_policy_month
programs used: clean_cc_policy
*/
********************************************************************************
*Clean state level concealed carry policy data for merge

*Bring in data
use "Raw Data/Policy Data/gunpolicy.dta", clear

** BRADY

gen brady_wait = 0 
replace brady_wait = 1 if brady_instant == 0

	
** PERMITS **

gen PERMIT = 0
replace PERMIT = 1 if permit > 0
replace PERMIT = . if permit == . 

** CORRECTIONS FROM POLIQUIN

replace PERMIT = 1 if state == "Missouri"
replace PERMIT = 0 if state == "Michigan" & year >= 2013

egen sample_month = group(year month)
replace brady_wait = 1 if state == "Ohio" & sample_month>=290 & sample_month <= 347

** CONCEALED CARRY **

gen conceal_carry = 0
replace conceal_carry = 1 if concealed_carry > 0

gen unrestricted = 0
replace unrestricted = 1 if concealed_carry == 1

gen shall_issue = 0
replace shall_issue = 1 if concealed_carry == 2

gen may_issue = 0
replace may_issue = 1 if concealed_carry == 3

gen prohibited = 0
replace prohibited = 1 if concealed_carry == 4


** DEALER CHECKS **

gen DEALER_CHECK = 0
replace DEALER_CHECK = 1 if dealer_checks > 0
replace DEALER_CHECK = . if dealer_checks == .
	
** BACKGROUND CHECKS **

gen BACKGROUND_CHECK = 0
replace BACKGROUND_CHECK = 1 if bg_check > 0
replace BACKGROUND_CHECK = . if bg_check ==.

** WAITING PERIODS **

gen ANY_WAIT = 0
replace ANY_WAIT = 1 if handgun_wait > 0 & longgun_wait > 0


** CORRECTIONS FROM POLIQUIN
replace BACKGROUND_CHECK = 1 if state == "Missouri"


** Collapse policies
collapse (max) brady_wait PERMIT (sum) ANY_WAIT BACKGROUND_CHECK DEALER_CHECK unrestricted shall_issue may_issue prohibited, ///
	by(fips usps state month year)
	
/* need to recode concealed carry vars such that policy that was in place 
for majority of month is treatment assigned to state */

foreach var of varlist unrestricted shall_issue may_issue prohibited ANY_WAIT BACKGROUND_CHECK DEALER_CHECK {
*jan, march, may, july, aug, oct, dec
if inlist(month,1,3,5,7,8,10,12) {
replace `var'=0 if `var'<=15
replace `var'=1 if `var'>15
}
*apr,june,sep,nov
else if inlist(month,4,6,9,11) {
replace `var'=0 if `var'<15
replace `var'=1 if `var'>=15
}
*feb
*leap years
else if month==2 & inlist(year,1992,1996,2000,2004,2008,2012) {
replace `var'=0 if `var'<=14
replace `var'=1 if `var'>14
}
*non leap years
else {
replace `var'=0 if `var'<14
replace `var'=1 if `var'>=14
}
}

replace state = lower(state)

save "Processed Data/monthly_gun_policies_UCR.dta", replace

